import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { AbstractForm } from '../../../../abstract/abstract-form';
import { Outlay } from '../../../../models/outlay';

@Component({
  selector: 'app-form-test',
  templateUrl: './form-test.component.html',
  styleUrls: ['./form-test.component.scss']
})
export class FormTestComponent extends AbstractForm<Outlay, any> implements OnInit {

  formGroup!: FormGroup;
  labelSpan = 8;
  contSpan = 16;
  @Input() data!: Outlay;
  @Input() submit!: (value: Outlay) => Observable<any>;

  constructor(
    private fb: FormBuilder,
  ) {
    super();
  }

  ngOnInit(): void {
    this.formGroup = this.fb.group({
      name: [
        '',
        [
          Validators.required,
          (c: FormControl) => {
            return c.value === '128' ? { len128: 'outlay.modify.name_required' } : null;
          }
        ]
      ],
      type: ['', [Validators.required]],
      money: [0],
      time: [''],
      mark: ['']
    });
    this.formGroup.patchValue(this.data);
  }
}
